package models

import (
	"github.com/astaxie/beego/orm"
	"time"
)

type Admin struct {
	Id         int `orm:"auto"`
	Username   string
	Password   string
	Name       string    `orm:"null"`
	Token      string    `orm:"null"`
	Expire_at  time.Time `orm:"type(datetime)"`
	Image      string    `orm:"null"`
	Created_at time.Time `orm:"auto_now_add;type(datetime)"`
	Updated_at time.Time `orm:"auto_now;type(datetime)"`
}

func AdminList(page int, limit int) ([]*Admin, int64) {
	admin := new(Admin)
	offset := (page - 1) * limit
	list := make([]*Admin, 0)
	total, _ := orm.NewOrm().QueryTable(admin).Count()
	orm.NewOrm().QueryTable(admin).Limit(limit, offset).All(&list)
	return list, total
}

func AdminById(Id int) (*Admin, error) {
	a := new(Admin)
	return a, orm.NewOrm().QueryTable(a).Filter("id", Id).One(a)
}

func AdminByUsername(Username string) (*Admin, error) {
	a := new(Admin)
	return a, orm.NewOrm().QueryTable(a).Filter("username", Username).One(a)
}
